DEFINE VARIABLE nprice AS DECIMAL.
DEFINE VARIABLE nprice2 AS DECIMAL.
DEFINE VARIABLE vend AS CHARACTER.
DEFINE VARIABLE s_date AS DATE.
DEFINE VARIABLE e_date AS DATE.

e_date = TODAY.
s_date = TODAY - 90.


/*     FOR EACH pc_mstr WHERE pc_part = "272-1060801" AND pc_list <> "S15032" AND pc_amt[1] <> 0                            */
/*                        AND ((pc_start <= s_date AND (pc_expire >= e_date OR pc_expire = ?))                              */
/*                              OR ((s_date <= pc_start AND pc_start <= e_date) AND (pc_expire >= e_date OR pc_expire = ?)) */
/*                              OR (s_date <= pc_expire AND pc_expire <= e_date)                                            */
/*                            ) NO-LOCK                                                                                     */
/*     BREAK BY pc_part BY pc_list BY pc_start BY pc_expire:                                                                */
/*         DISP pc_list pc_part pc_um pc_curr pc_start pc_expire pc_amt[1] .                                                */
/*     END.                                                                                                                 */
/*                                                                                                                          */
/*     IF NOT AVAILABLE pc_mstr THEN DO:                                                                                    */
/*        FOR EACH pc_mstr WHERE pc_part = "272-1060801":                                                                   */
/*            DISP pc_list pc_part pc_um pc_curr pc_start pc_expire pc_amt[1] .                                             */
/*        END.                                                                                                              */
/*     END.                                                                                                                 */

RUN findNewPrice (INPUT "RMB",INPUT e_date - 30,INPUT e_date,INPUT "224-0901501",OUTPUT vend, OUTPUT nprice,  OUTPUT nprice2).  
/*  IF nprice = 0 THEN                                                                                                */
/*     RUN findNewPrice (INPUT "RMB",INPUT s_date - 270,INPUT s_date,INPUT "150-2304005",OUTPUT vend, OUTPUT nprice). */
 DISP vend nprice FORMAT ">>>>9.9<<<<" nprice2  FORMAT ">>>>9.9<<<<" .                                                                                         

/*create the exchange rate of currency */
PROCEDURE findNewPrice.
    DEFINE INPUT PARAMETER base_curr  AS CHARACTER.
    DEFINE INPUT PARAMETER s_date AS DATE.
    DEFINE INPUT PARAMETER e_date AS DATE.
    DEFINE INPUT PARAMETER part   AS CHARACTER.
    DEFINE OUTPUT PARAMETER newVend   AS CHARACTER.
    DEFINE OUTPUT PARAMETER newPrice AS DECIMAL.
    DEFINE OUTPUT PARAMETER newPrice2 AS DECIMAL.

    DEFINE VARIABLE price_min LIKE pc_amt[0] INITIAL 99999.
    DEFINE VARIABLE price_min2 LIKE pc_amt[0] INITIAL 99999.
    DEFINE VARIABLE price_max LIKE pc_amt[0] INITIAL -99999.
    DEFINE VARIABLE vend_min LIKE pc_list INITIAL "".
    DEFINE VARIABLE vend_max LIKE pc_list INITIAL "".
    DEFINE VARIABLE start_min LIKE pc_start INITIAL ?.
    DEFINE VARIABLE start_max LIKE pc_start INITIAL ?.
    DEFINE VARIABLE um_min LIKE pc_um INITIAL ?.
    DEFINE VARIABLE um_max LIKE pc_um INITIAL ?.
    DEFINE VARIABLE curr_min LIKE pc_curr INITIAL ?.
    DEFINE VARIABLE curr_max LIKE pc_curr INITIAL ?.
    DEFINE VARIABLE exc_rate AS DECIMAL.
    DEFINE VARIABLE um_rate AS DECIMAL.

    FOR EACH pc_mstr WHERE pc_part = part AND pc_list <> "S15032" AND pc_amt[1] <> 0
                       AND ((s_date <= pc_start AND pc_expire >= e_date )
                             OR ((s_date <= pc_start AND pc_start <= e_date) AND (pc_expire >= e_date OR pc_expire = ?))
                             OR (s_date <= pc_expire AND pc_expire <= e_date)
                           ) NO-LOCK 
    BREAK BY pc_part BY pc_list BY pc_start BY pc_expire:

      IF FIRST-OF (pc_part) THEN DO:
         price_min = 99999.
         price_max = -99999.
         vend_min = "".
         vend_max = "".
      END.
DISP pc_part pc_list pc_start pc_amt[1] pc_curr WITH WIDTH 130.
      IF LAST-OF (pc_list) THEN DO:

         IF base_curr = pc_curr THEN
            exc_rate = 1.0.
         ELSE DO:
            RUN getCurrRate(INPUT base_curr, INPUT pc_curr, INPUT s_date, INPUT e_date, OUTPUT exc_rate).
            exc_rate = exc_rate * 1.17. /*if the currency is not base so the price is not tax_in */
         END.

         FIND FIRST pt_mstr WHERE pt_part = pc_part NO-LOCK NO-ERROR.
         IF AVAILABLE pt_mstr THEN 
            RUN getItemConvUm(INPUT pc_part,INPUT pt_um, INPUT pc_um, OUTPUT um_rate).
         ELSE
            um_rate = 1.

         IF price_min > (pc_amt[1] * exc_rate / um_rate) THEN DO:
            price_min2 = price_min.
            price_min = pc_amt[1] * exc_rate / um_rate .
            vend_min = pc_list.
         END.
         ELSE IF price_min2 > (pc_amt[1] * exc_rate / um_rate) THEN DO:
            price_min2 = pc_amt[1] * exc_rate / um_rate.
         END.

         IF price_max < (pc_amt[1] * exc_rate / um_rate) THEN DO:
            price_max = pc_amt[1] * exc_rate / um_rate.
            vend_max = pc_list.
         END.
DISP price_min LABEL "MIN" price_min2  LABEL "MIN2" vend_min  WITH WIDTH 130.
      END.
      
/*       IF LAST-OF (pc_list) THEN DO:                                      */
/*          ACCUMULATE pc_amt[1] / um_rate (COUNT BY pc_part).              */
/*          ACCUMULATE pc_amt[1] * exc_rate / um_rate (AVERAGE BY pc_part). */
/*       END.                                                               */
/*                                                                          */
      IF LAST-OF(pc_part) THEN DO:
         newVend = Vend_min.
         newPrice = price_min.
         IF price_min2 = 99999 THEN
            newPrice2 = price_min.
         ELSE
            newPrice2 = price_min2.
      END.

    END. 

    IF NOT AVAILABLE(pc_mstr) OR vend_min = "" OR price_min = 99999 THEN DO:
       FOR EACH pc_mstr WHERE pc_part = part AND pc_list <> "S15032" AND pc_amt[1] <> 0 NO-LOCK
       BREAK BY pc_part BY pc_start BY pc_expire BY pc_amt[1] DESC:
DISP pc_list pc_part pc_start pc_expire pc_amt[1].
           IF LAST-OF (pc_start) THEN DO:
               newVend = pc_list.
               newPrice = pc_amt[1].
               newPrice2 = newPrice.
           END.

       END.

    END.


END PROCEDURE.


PROCEDURE getCurrRate.
    DEFINE INPUT PARAMETER base_curr  AS CHARACTER.
    DEFINE INPUT PARAMETER targ_curr  AS CHARACTER.
    DEFINE INPUT PARAMETER s_date AS DATE.
    DEFINE INPUT PARAMETER e_date AS DATE.
    DEFINE OUTPUT PARAMETER exc_rate AS DECIMAL.
    
    FOR EACH exr_rate WHERE (exr_start_date <= e_date AND e_date <= exr_end_date)  OR e_date = ? NO-LOCK:

        IF exr_curr1 = base_curr AND exr_curr2 = targ_curr THEN DO:
           exc_rate = exr_rate.
           RETURN.
        END.
        ELSE IF exr_curr2 = base_curr AND exr_curr1 = targ_curr THEN DO:
           exc_rate = exr_rate2.
           RETURN.
        END.
    END.

END PROCEDURE.

PROCEDURE getItemConvUm.
    DEFINE INPUT  PARAMETER part AS CHARACTER.
    DEFINE INPUT  PARAMETER um1 AS CHARACTER.   /*stock base unit */
    DEFINE INPUT  PARAMETER um2 AS CHARACTER.   /*conver unit*/
    DEFINE OUTPUT PARAMETER rate AS DECIMAL.

    FIND FIRST um_mstr NO-LOCK WHERE um_part = part AND um_um = um1 AND um_alt_um = um2 NO-ERROR .
    IF AVAILABLE um_mstr THEN
       rate = um_conv .
    ELSE DO:
       FIND FIRST um_mstr NO-LOCK WHERE um_um = um1 AND um_alt_um = um2 NO-ERROR .
       IF AVAILABLE um_mstr THEN
          rate = um_conv .
       ELSE 
          rate = 1 .
    END.
END PROCEDURE.

